package com.geoboard;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.views.overlay.ScaleBarOverlay;

import android.graphics.Color;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

/**
 * The entry point for the application.
 * @author Gordon Meyer 02/13
 */
public class MainActivity extends Activity {
	/*************************************************************
	 * 		Members
	 */
	
	//private GoogleMap map = null;
	//private MapFragment fragMap = null;
	private EditText edtxtMessages = null;
	private MapView mapView = null;
    private MapController mapController = null;
    private MyLocationOverlay myLocationOverlay = null;
			
	/*************************************************************
	 * 		Event Handlers
	 */
	
	
	/**** Activity Events ****/
	
	/**
	 * Android onCreate event.
	 */
    @SuppressWarnings("deprecation")
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);               
        setContentView(R.layout.activity_main);
        
        // initialize global variables
        edtxtMessages = (EditText)findViewById(R.id.edtxtMessage);
        
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setTileSource(TileSourceFactory.MAPNIK);
        mapView.setBuiltInZoomControls(true);
        mapView.setMultiTouchControls(true);
        
        mapController = mapView.getController();
        mapController.setZoom(19);
        GeoPoint gPt = new GeoPoint(51500000, -150000);
        //Centre map near to Hyde Park Corner, London
        //mapController.setCenter(gPt);
        
        myLocationOverlay = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(myLocationOverlay);
        
        ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this.getBaseContext());
        myScaleBarOverlay.enableScaleBar();
        mapView.getOverlays().add(myScaleBarOverlay);
        
        //mapView.postInvalidate();
        
        
        Button b = (Button)findViewById(R.id.btnPost);
        
        // set button onClick event to post a message
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	String s = edtxtMessages.getText().toString();
            	if(MessageManager.postMessage(s)){
            		edtxtMessages.setEnabled(false);
            	}
            		
            }
        });
        
        // tabs
        TabHost tabHost=(TabHost)findViewById(R.id.tabhost);
        tabHost.setup();

        TabSpec messTab=tabHost.newTabSpec("Messages");
        messTab.setContent(R.id.messageTab);
        messTab.setIndicator("Messages");
        
        TabSpec mapTab = tabHost.newTabSpec("Map");
        mapTab.setContent(R.id.mapTab);
        mapTab.setIndicator("Map");        
        
        tabHost.addTab(mapTab);
        tabHost.addTab(messTab);    
        
        // for each tab change the background appearance and font
        for(int i = 0; i < 2; i++){
        	tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selector));
        	TextView tv = (TextView)tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
        	tv.setTextColor(Color.WHITE);
        }
        
    }
    
    /**
     * Android onStart Event
     */
    @Override
    protected void onStart(){
    	super.onStart();
    	Globals.setMainActivity(this);
    	
    	// pass event along
    	PositionManager.onStart();
    	//fragMap.onStart();    	
    }
    
    /**
     * Android onResume event
     */
    @Override
    protected void onResume() {
    	super.onResume();
    	
    	PositionManager.onResume();
    	
    	myLocationOverlay.enableMyLocation();
    	myLocationOverlay.enableCompass();
    	
    	
    	GeoPoint gp = new GeoPoint(PositionManager.getLocation());
    	
    	mapController.setCenter(gp);
    	
    	// pass the event along
    	
    	//fragMap.onResume();
    	
    	// Begin message retrieval
    	//MessageManager.getMessagesOnTimer();
    }
    
    /**
     * Android onPause event
     */
    @Override 
    protected void onPause(){
    	super.onPause();
    	
    	myLocationOverlay.disableMyLocation();
    	myLocationOverlay.disableCompass();
    	
    	// pass the event along
    	PositionManager.onPause();
    	//fragMap.onPause();
    	
    	// stop retrieving messages
    	//MessageManager.stopMessagesTimer();
    }
    
    /**
     * Android onStop event
     */
    @Override 
    protected void onStop(){
    	super.onStop();
    	
    	// pass the event along
    	//fragMap.onStop();
    }    
    
    /**
     * Android onCreateOptionsMenu event.  This app has no
     * entries in the options menu.
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }	
}
